package com.huanggf.meituan.fooddetail;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huanggf.meituan.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class FoodDetailAdapter extends BaseAdapter {
    private List<FoodDetails> foodDetailsList;
    private Context context;

    public FoodDetailAdapter(Context context, List<FoodDetails> foodDetailsList) {
        this.context = context;
        this.foodDetailsList = foodDetailsList;
    }

    @Override
    public int getCount() {
        return foodDetailsList.size();
    }

    @Override
    public Object getItem(int position) {
        return foodDetailsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.food_detail_item, null);
            viewHolder.shopLogo = (ImageView) convertView.findViewById(R.id.shop_logo);
            viewHolder.shopName = (TextView) convertView.findViewById(R.id.shop_name);
            viewHolder.grade = (TextView) convertView.findViewById(R.id.grade);
            viewHolder.sellNumber = (TextView) convertView.findViewById(R.id.sell_number);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.distance = (TextView) convertView.findViewById(R.id.distance);
            viewHolder.startPrice = (TextView) convertView.findViewById(R.id.reserve_time);
            viewHolder.price = (TextView) convertView.findViewById(R.id.price);
            viewHolder.avgPrice = (TextView) convertView.findViewById(R.id.avgPrice);
            viewHolder.ad1 = (TextView) convertView.findViewById(R.id.ad1);
            viewHolder.ad2 = (TextView) convertView.findViewById(R.id.ad2);
            viewHolder.discount1 = (TextView) convertView.findViewById(R.id.discount1);
            viewHolder.discount2 = (TextView) convertView.findViewById(R.id.discount2);
            viewHolder.discount3 = (TextView) convertView.findViewById(R.id.discount3);
            viewHolder.discount4 = (TextView) convertView.findViewById(R.id.discount4);
            viewHolder.sendTypeImg = (ImageView) convertView.findViewById(R.id.send_type_img);

            FoodDetails foodDetails = foodDetailsList.get(position);
            try {
                if(null != foodDetails.getShopLogo() && !foodDetails.getShopLogo().trim().equals(""))
                    viewHolder.shopLogo.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(foodDetails.getShopLogo())));
                viewHolder.shopName.setText(foodDetails.getShopName());
                viewHolder.grade.setText(String.valueOf(foodDetails.getGrade()));
                viewHolder.sellNumber.setText(String.valueOf(foodDetails.getSellNumber()));
                viewHolder.time.setText(foodDetails.getTime());
                viewHolder.distance.setText(foodDetails.getDistance());
                viewHolder.startPrice.setText(foodDetails.getStartPrice());
                viewHolder.price.setText(foodDetails.getPrice());
                viewHolder.avgPrice.setText(foodDetails.getAvgPrice());
                viewHolder.ad1.setText(foodDetails.getAd1());
                viewHolder.ad2.setText(foodDetails.getAd2());
                viewHolder.discount1.setText(foodDetails.getDiscount1());
                viewHolder.discount2.setText(foodDetails.getDiscount2());
                viewHolder.discount3.setText(foodDetails.getDiscount3());
                viewHolder.discount4.setText(foodDetails.getDiscount4());
                if(null != foodDetails.getSendTypePath() && !foodDetails.getSendTypePath().trim().equals(""))
                    viewHolder.sendTypeImg.setImageBitmap(BitmapFactory.decodeStream(new FileInputStream(foodDetails.getSendTypePath())));
                convertView.setTag(viewHolder);
            } catch (FileNotFoundException e) {
                Log.e("FileNotFoundException", e.getMessage());
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView shopLogo;
        private TextView shopName;
        private TextView grade;
        private TextView sellNumber;
        private TextView time;
        private TextView distance;
        private TextView startPrice;
        private TextView price;
        private TextView avgPrice;
        private TextView ad1;
        private TextView ad2;
        private TextView discount1;
        private TextView discount2;
        private TextView discount3;
        private TextView discount4;
        private ImageView sendTypeImg;
    }
}
